/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    i2s.c
  * @brief   This file provides code for the configuration
  *          of the I2S instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "i2s.h"

/* USER CODE BEGIN 0 */
#include <stdio.h>

/* USER CODE END 0 */

I2S_HandleTypeDef hi2s2;
DMA_HandleTypeDef hdma_spi2_tx;

/* I2S2 init function */
void MX_I2S2_Init(void)
{

	/* USER CODE BEGIN I2S2_Init 0 */

	// hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_22K;
	// hi2s2.Init.CPOL = I2S_CLOCK_EXTERNAL;  //  I2S_CLOCK_PLL
	// I2S_CLOCK_PLL

	/* USER CODE END I2S2_Init 0 */

	/* USER CODE BEGIN I2S2_Init 1 */

	/* USER CODE END I2S2_Init 1 */
	hi2s2.Instance = SPI2;
	hi2s2.Init.Mode = I2S_MODE_MASTER_TX;
	hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
	hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
	hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
	hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_44K;
	hi2s2.Init.CPOL = I2S_CPOL_LOW;
	hi2s2.Init.ClockSource = I2S_CLOCK_EXTERNAL;
	hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
	if (HAL_I2S_Init(&hi2s2) != HAL_OK) {
		Error_Handler();
	}
	/* USER CODE BEGIN I2S2_Init 2 */
	//   printf("i2sclk = %u i2sdiv = %u i2sodd = %u\r\n", i2sclk,  i2sdiv,  i2sodd);

	// __HAL_RCC_I2S_CONFIG(I2S_CLOCK_EXTERNAL);

	/* USER CODE END I2S2_Init 2 */

}

void HAL_I2S_MspInit(I2S_HandleTypeDef *i2sHandle)
{

	GPIO_InitTypeDef GPIO_InitStruct = {0};
	if(i2sHandle->Instance == SPI2) {
		/* USER CODE BEGIN SPI2_MspInit 0 */
#if 0
		RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

		if(hi2s2.Init.AudioFreq == 96000) {
			PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
			PeriphClkInitStruct.PLLI2S.PLLI2SN = 344;
			PeriphClkInitStruct.PLLI2S.PLLI2SM = 25;
			PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
		} else if(hi2s2.Init.AudioFreq == 48000) {
			PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
			PeriphClkInitStruct.PLLI2S.PLLI2SN = 258;
			PeriphClkInitStruct.PLLI2S.PLLI2SM = 25;
			PeriphClkInitStruct.PLLI2S.PLLI2SR = 3;
		} else {
			PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
			PeriphClkInitStruct.PLLI2S.PLLI2SN = 271;
			PeriphClkInitStruct.PLLI2S.PLLI2SM = 25;
			PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
		}

		if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
			Error_Handler();
		}
#endif

		// RCC_I2SCLKSOURCE_EXT
		// RCC_I2SCLKSOURCE_PLLI2S
		
		RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

		if(hi2s2.Init.AudioFreq == 96000) {

			__HAL_RCC_SPI2_CLK_DISABLE();

			__HAL_RCC_I2S_CONFIG(0x00000000U);

			PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
			PeriphClkInitStruct.PLLI2S.PLLI2SN = 344;
			PeriphClkInitStruct.PLLI2S.PLLI2SM = 25;
			PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
			if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
				Error_Handler();
			}
		} else if(hi2s2.Init.AudioFreq == 48000) {

			__HAL_RCC_SPI2_CLK_DISABLE();

			__HAL_RCC_I2S_CONFIG(0x00000000U);

			PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
			PeriphClkInitStruct.PLLI2S.PLLI2SN = 258;
			PeriphClkInitStruct.PLLI2S.PLLI2SM = 25;
			PeriphClkInitStruct.PLLI2S.PLLI2SR = 3;
			if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
				Error_Handler();
			}
		} else {
			
			__HAL_RCC_SPI2_CLK_DISABLE();

			__HAL_RCC_I2S_CONFIG(0x1UL << 23U);  //  0x800000
		}

		printf("---> RCC_I2SCLKSOURCE_PLLI2S = 0x%x  RCC_I2SCLKSOURCE_EXT = 0x%lx\r\n", RCC_I2SCLKSOURCE_PLLI2S, RCC_I2SCLKSOURCE_EXT);
		
		printf("---> I2S_SRC = 0x%x\r\n", __HAL_RCC_GET_I2S_SOURCE());

		/* USER CODE END SPI2_MspInit 0 */
		/* I2S2 clock enable */
		__HAL_RCC_SPI2_CLK_ENABLE();

		__HAL_RCC_GPIOB_CLK_ENABLE();
		__HAL_RCC_GPIOC_CLK_ENABLE();
		/**I2S2 GPIO Configuration
		PB12     ------> I2S2_WS
		PB13     ------> I2S2_CK
		PB15     ------> I2S2_SD
		PC6     ------> I2S2_MCK
		*/
		GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_15;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
		GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
		HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

		GPIO_InitStruct.Pin = GPIO_PIN_6;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
		GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

		/* I2S2 DMA Init */
		/* SPI2_TX Init */
		hdma_spi2_tx.Instance = DMA1_Stream4;
		hdma_spi2_tx.Init.Channel = DMA_CHANNEL_0;
		hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
		hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
		hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
		hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
		hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
		hdma_spi2_tx.Init.Mode = DMA_CIRCULAR;
		hdma_spi2_tx.Init.Priority = DMA_PRIORITY_LOW;
		hdma_spi2_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
		hdma_spi2_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
		hdma_spi2_tx.Init.MemBurst = DMA_MBURST_SINGLE;
		hdma_spi2_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
		if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK) {
			Error_Handler();
		}

		__HAL_LINKDMA(i2sHandle, hdmatx, hdma_spi2_tx);

		/* I2S2 interrupt Init */
		HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
		HAL_NVIC_EnableIRQ(SPI2_IRQn);
		/* USER CODE BEGIN SPI2_MspInit 1 */

		/* USER CODE END SPI2_MspInit 1 */
	}
}

void HAL_I2S_MspDeInit(I2S_HandleTypeDef *i2sHandle)
{

	if(i2sHandle->Instance == SPI2) {
		/* USER CODE BEGIN SPI2_MspDeInit 0 */

		/* USER CODE END SPI2_MspDeInit 0 */
		/* Peripheral clock disable */
		__HAL_RCC_SPI2_CLK_DISABLE();

		/**I2S2 GPIO Configuration
		PB12     ------> I2S2_WS
		PB13     ------> I2S2_CK
		PB15     ------> I2S2_SD
		PC6     ------> I2S2_MCK
		*/
		HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_15);

		HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6);

		/* I2S2 DMA DeInit */
		HAL_DMA_DeInit(i2sHandle->hdmatx);

		/* I2S2 interrupt Deinit */
		HAL_NVIC_DisableIRQ(SPI2_IRQn);
		/* USER CODE BEGIN SPI2_MspDeInit 1 */

		/* USER CODE END SPI2_MspDeInit 1 */
	}
}

/* USER CODE BEGIN 1 */

/* I2S2 init function */
void MY_MX_I2S2_Init(uint32_t DataSize, uint32_t SampleRate)
{
	HAL_I2S_DeInit(&hi2s2);

	hi2s2.Instance = SPI2;
	hi2s2.Init.Mode = I2S_MODE_MASTER_TX;
	hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
	hi2s2.Init.DataFormat = DataSize;
	hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
	hi2s2.Init.AudioFreq = SampleRate;
	hi2s2.Init.CPOL = I2S_CPOL_LOW;
	hi2s2.Init.ClockSource = I2S_CLOCK_PLL;
	hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
	if (HAL_I2S_Init(&hi2s2) != HAL_OK) {
		Error_Handler();
	}

}

/* USER CODE END 1 */
